# -*- encoding: utf-8 -*-

import re
from uuid import *
import json
from json import encoder
from genuid import *
from flask.json import jsonify
with open("d:\menu.json",'r',encoding='utf-8') as f:
  content = f.read()

def done(m):    
    return "\""+m.group(1)+"\":"
   
content = re.sub("(\w*):",done,content)  
content = re.sub("//.*",'',content)  
content = re.sub("\(\) =>",'',content)
content = re.sub("import \('@/views/","\"",content)
content = re.sub("'\),","\",",content)
content = re.sub("Layout","'Layout'",content)
content = re.sub("true","True",content)
res = eval(content)

# print(res[0])
# menuId pid  title  path  name  redirect  component  
# alwaysShow  hidden  noCache  activeMenu  icon  type  sort 

dataList = []
def genTable(mlis,pid = ""):
  for m in mlis:
    r = {}
    r['pid'] = pid
    mid = getUid()
    r['menuId']     = mid
    r['path']       = m['path']
    r['component']  = m['component']
    r['redirect']   = m['redirect'] if "redirect" in m else ""
    r['name']       = m['name'] if "name" in m else ""
    r['alwaysShow'] = f"{m['alwaysShow']}" if "alwaysShow" in m else ""    
    r['hidden']     = f"{m['hidden']}" if "hidden" in m else ""    
    if "meta" in m:
      r['title']      = m['meta']['title'] if "title" in m['meta'] else ""
      r['icon']       = m['meta']['icon'] if "icon" in m['meta'] else ""         
      r['activeMenu'] = m['meta']['activeMenu'] if "activeMenu" in m['meta'] else ""      
      r['noCache']    = f"{m['meta']['noCache']}" if "noCache" in m['meta'] else ""           
    dataList.append(r)
    if "children" in m:
      genTable(m['children'],mid)

genTable(res,"")
print("\n",json.dumps(dataList,ensure_ascii=False))

